package com.tubeonfire.controller;

import java.io.IOException;
import java.util.TreeMap;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;

import com.tubeonfire.entity.Feedback;
import com.tubeonfire.model.admin.FeedbackModel;
import com.tubeonfire.util.IdUniqueHelper;

@SuppressWarnings("serial")
public class ContactServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(ContactServlet.class
			.getName());

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {
			req.setAttribute("url", req.getRequestURL().toString());
			req.getRequestDispatcher("/contact_us.jsp").forward(req, resp);
		} catch (Exception e) {
			log.warning(e.toString());
			e.printStackTrace(System.err);
			resp.sendError(4000,
					"Website is down for maintenance. We are sorry for the inconvenience !");
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			Feedback obj = new Feedback();
			TreeMap<String, String> check = new TreeMap<String, String>();
			String name = req.getParameter("name");
			String email = req.getParameter("email");
			String title = req.getParameter("title");
			String message = req.getParameter("message");

			// check name.
			if (name == null || name.length() == 0) {
				check.put("name", "Please enter your name.");
			} else {
				obj.setName(name);
			}

			// check mail
			if (email == null || email.length() == 0) {
				check.put("email", "Please enter your email.");
			} else {
				Pattern pattern = Pattern.compile(".+@.+\\.[a-z]+");
				Matcher m = pattern.matcher(email);
				boolean matchFound = m.matches();
				if (!matchFound) {
					check.put("email", "Please enter exactly email.");
				} else {
					obj.setEmail(email);
				}
			}

			// check title.
			if (title == null || title.length() == 0) {
				check.put("title", "Please enter message title.");
			} else {
				obj.setTitle(title);
			}

			// check message.
			if (message == null || message.length() == 0) {
				check.put("message", "Please enter message content.");
			} else {
				obj.setMessage(message);
			}

			// check cap.cha
			String remoteAddr = req.getRemoteAddr();
			ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
			reCaptcha.setPrivateKey("6LfUNtESAAAAANSiC1e3dD974bMUFY21S5QANakS");
			String challenge = req.getParameter("recaptcha_challenge_field");
			String uresponse = req.getParameter("recaptcha_response_field");
			ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(
					remoteAddr, challenge, uresponse);

			if (!reCaptchaResponse.isValid()) {
				check.put("capcha", "Please enter exactly what you see.");
			}
			if (check.size() > 0) {
				req.setAttribute("feedback", obj);
				req.setAttribute("check", check);
			} else {
				obj.setStatus(1);
				obj.setId(IdUniqueHelper.getId());
				FeedbackModel.insert(obj);
				FeedbackModel.clearModelCache();
				req.setAttribute("success",
						"Your feedback has been sent, we'll reply as soon as possible.");
			}
			req.getRequestDispatcher("/contact_us.jsp").forward(req, resp);
		} catch (Exception e) {
			log.warning(e.toString());
			e.printStackTrace(System.err);
			resp.sendError(4000,
					"Website is down for maintenance. We are sorry for the inconvenience !");
		}
	}
}
